package com.jt.controller;

import com.jt.pojo.SearchResult;
import com.jt.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.UnsupportedEncodingException;

/**
 * @author dc
 * @Classname SearchController
 * @Date 2020/3/27 11:03
 */
@Controller
public class SearchController {
    @Autowired
    SearchService searchService;

    @RequestMapping("/search")
    public String search(String q
            , @RequestParam(defaultValue = "1",required =false) Integer page
            , @RequestParam(defaultValue = "20",required = false) Integer rows
            , Model model) throws UnsupportedEncodingException {
        SearchResult itemByKey = searchService.findItemByKey(q, page, rows);
        //回显查询条件
        model.addAttribute("query",q);
        model.addAttribute("itemList",itemByKey.getItemList());
        model.addAttribute("page",page);
        model.addAttribute("totalPages",itemByKey.getPageCount());
        model.addAttribute("rows",rows);
        model.addAttribute("recordCount",itemByKey.getRecordCount());
        if(page<itemByKey.getPageCount()){
            model.addAttribute("nextPage",page+1);
        }
        if(page>1){
            model.addAttribute("lastPage",page-1);
        }
        return "search";
    }


}
